package demo11;

import org.junit.Test;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @description:JDK代理
 * @author: apple
 * @date:  2022/2/3 7:39 下午
 * @version 1.0
 **/
public class UserDaoTest {

    @Test
    public void test1() {

        final UserDaoImpl impl = new UserDaoImpl();

        final UserDao userDao = (UserDao)Proxy.newProxyInstance(UserDaoTest.class.getClassLoader(), new Class[]{UserDao.class}, new InvocationHandler() {

            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

                System.out.println("执行前");

                final Object result = method.invoke(impl, args);

                System.out.println("执行后");
                return result;
            }
        });
        final int sum = userDao.add(5, 4);
        System.out.println(sum);

    }
}
